package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.ui.UIWindows;

/**
 * 菜单控件
 * @author handinfo
 */
public class DWPopMenu extends DWControl
{

//	public static final int ALIGN_LEFT = 0;
//	public static final int ALIGN_CENTRE = 1;
//	public static final int ALIGN_RIGHT = 2;
//	protected static final int W_MIN_CONTENT = 1;//最小内容层显示宽度,屏幕的1/4
//	protected static final int W_MAX_CONTENT = (DWGameManager.Screen_Width * 3/4);//最大内容层显示宽度,屏幕的3/4
//	private final int IW = 3;//内容层离rect层的宽度
//	private int m_w_content = 0;
//	private int m_h_content = 0;
//	private final int INITX_CONTENT = 8;//内容层的起始绘制x
//	private final int INITY_CONTENT = 8;//内容层的起始绘制x
//	private final int IH = 20;// 框与字之间的间隔
//	private String[] m_menus = null;
//	private String[][] m_strs;
//	private int m_index_menu = 0;// 选中索引
//	private int m_anchor;
//	private int m_w_rect;
//	private final int COLOR_WORD_YES = 0xFFF8EC89;
//	private final int COLOR_WORD_NO = 0xFFFFFFFF;
//	public boolean m_guide = false;//游戏引导

	private int m_index_menu = 0;// 选中索引
//	private String[] m_menus = null;
	private DWBackground m_background = null;
	private DWButton[] m_buttons = null;
	private final int GAP_W_BB = 20;
	private final int GAP_H_BB = 10;
//	private Bitmap img_corner = null;
//	private Bitmap img_side = null;

	/**
	 * 创建菜单
	 * @param menus
	 */
	public DWPopMenu(String[] menus)
	{
//		setPopmenus(menus, ALIGN_LEFT);
		this(menus, null);
	}

	public DWPopMenu(String[] menus, Bitmap background)
	{
		if (menus == null) {
			throw new NullPointerException("DWPopMenu menus is null!");
		}
		if (background == null) {
			throw new NullPointerException("DWPopMenu background is null!");
		}
		img_background = background;
		int button_w = background.getWidth();
		int button_h = background.getHeight();
		m_buttons = new DWButton[menus.length];
		for (int i = 0; i < menus.length; i++) {
			m_buttons[i] = new DWButton(menus[i], background, GAP_W_BB, i * (button_h + GAP_H_BB * 2) + GAP_H_BB);
		}
		int bw = button_w + GAP_W_BB * 2;
		int bh = menus.length * (button_h + GAP_H_BB * 2);
		m_background = new DWBackground(UIWindows.createImage("/img/newui/beijing_1.gnp"), bw, bh);
		setShowWideHigh(m_background.m_show_w, m_background.m_show_h);
		setCentre();
	}
	/**
	 * 设置显示button的名字,必须与原长度相同
	 * @param menus
	 */
	public void setMenus(String[] menus) {
		for (int i = 0; i < m_buttons.length; i++) {
			m_buttons[i].setName(menus[i]);
		}
	}
	/**
	 * 设置按下后的图片
	 * @param img
	 */
	public void setDownImage(Bitmap img) {
		if (img != null && m_buttons != null) {
			for (int i = 0; i < m_buttons.length; i++) {
				if (m_buttons[i] != null) {
					m_buttons[i].setDownImage(img);
				}
			}
		}
	}
//	/**
//	 * 设置的menus只能是普通字符串数组而非超链接类型
//	 * @param menus
//	 * @param anchor
//	 */
//	public void setPopmenus(String[] menus, int anchor) {
//		if (menus == null) {
//			Tools.debugPrintln("未传入参数");
//			return ;
//		}
//		m_menus = menus;
//		m_index_menu = 0;// 替换内容时需进行修改
//		m_w_content = DWControlTools.getMidNum(DWControlTools.getMaxWidth(m_menus), W_MIN_CONTENT, W_MAX_CONTENT) + 1;// 避免刚好相等的情况
//		m_w_rect = m_w_content + (IW << 1);
//		m_strs = new String[menus.length][];
//		/**
//		 * 采用此种计算内容框高度的方式是基于以下两个原因 1：效率 2：在重新赋值的时候可以将原有的值带入目前的系统
//		 */
//		int sumH = 0;
//		for (int i = 0; i < menus.length; i++) {
//			if (menus[i] == null) {// 目前按空处理
//				throw new IllegalArgumentException("未传入参数 index = " + i);
//			}
//			m_strs[i] = DWControlTools.getStrArray(menus[i], DWFont.getDefaultFont(), m_w_content);
//			sumH += m_strs[i].length * DWControlsManager.H_ALL_FONT + (IH << 1);
//		}
//		m_h_content = sumH;
//		setShowWideHigh(m_w_rect + (INITX_CONTENT << 1), m_h_content + (INITY_CONTENT << 1));
//		m_anchor = anchor;
//		setCentre();
//	}

	public DWControl getButton(int index) {
		return m_buttons[index];
	}
	public void render(DWGraphics g)
	{

		m_background.renderLimits(g, m_show_x, m_show_y, m_show_w, m_show_h, 0, 0);
		for (int i = 0; i < m_buttons.length; i++) {
			m_buttons[i].renderLimits(g, m_show_x, m_show_y, m_show_w, m_show_h, 0, 0);
		}

//		if (m_strs == null) {
//			return;
//		}
//		
//		DWControlTools.drawWindowBackgroud(g, m_show_x - 2, m_show_y - 2, m_show_w + 4, m_show_h + 4);
//		g.setColor(COLOR_WORD_NO);
//		DWControlTools.paintBox(g, m_show_x, m_show_y, m_show_w - 1, m_show_h - 1);
//		g.setColor(COLOR_WORD_NO);
//		g.drawRect(m_show_x + 2, m_show_y + 2, m_show_w - 5, m_show_h - 5);
//		//
//		int tempX = m_show_x + INITX_CONTENT;
//		int tempY = m_show_y + INITY_CONTENT;
//		int contentX = tempX + IW;
//		int tempIndex = m_index_menu;
//		for (int i = 0, length = m_strs.length; i < length; i++) {
//			if (m_strs[i] == null) {
//				continue;
//			}
//			if (m_guide && i == tempIndex) {
////				UITools.paintYindaoDesxribe(g, tempX - 3, tempY - 3, m_w_rect + 6, m_strs[i].length * (DWFont.getDefaultFont().getFontHeight() + (IH << 1) + 6), m_strs[i]);
//			}
//			if (i == tempIndex) {
//				DWControlTools.paintSelItem(g, tempX, tempY, m_w_rect, m_strs[i].length * DWControlsManager.H_ALL_FONT + (IH << 1));
//			}
//			tempY += IH;// 相对框偏移三像素
//			tempY = renderItem(g, m_strs[i], contentX, tempY, i == tempIndex ? COLOR_WORD_YES : COLOR_WORD_NO);
//			tempY += IH;
//		}
	}

//	private int renderItem(DWGraphics g, String[] strs, int x, int y, int color)
//	{
//		int tempY = y;
//		if (strs == null) {
//			return tempY;
//		}
//		for (int j = 0; j < strs.length; j++) {
//			if (strs[j] == null) {
//				continue;
//			}
//			switch (m_anchor) {
//				case ALIGN_LEFT:
//					g.drawString(strs[j], color, x, tempY, Tools.LEFT_TOP);
////					g.setColor(Tools.WHITE);
////					g.drawRect(x, tempY, (int)DWFont.getDefaultFont().stringWidth(strs[j]), DWControlsManager.H_ALL_FONT + (IH << 1));
//					break;
//				case ALIGN_CENTRE:
//					g.drawString(strs[j], color, x +  (m_w_content >> 1), tempY, Tools.HCENTER_TOP);
//					break;
//				case ALIGN_RIGHT:
//					g.drawString(strs[j], color, x + (m_w_content), tempY, Tools.RIGHT_TOP);
//					break;
//				default:
//					throw new IllegalArgumentException("无此类型");
//			}
//			tempY += DWFont.getDefaultFont().getFontHeight();
//			
//		}
//		return tempY;
//	}

	public void logic()
	{

	}

	public int getIndex()
	{
		return m_index_menu;
	}

	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			for (int i = 0; i < m_buttons.length; i++) {
				if (m_buttons[i].doDown(new PointF(point.x - m_show_x, point.y - m_show_y))) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
		PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
		for (int i = 0; i < m_buttons.length; i++) {
			if (m_buttons[i].doDrag(newStart, newEnd, velocityX, velocityY)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
		PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
		for (int i = 0; i < m_buttons.length; i++) {
			if (m_buttons[i].doFling(newStart, newEnd, velocityX, velocityY)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			for (int i = 0; i < m_buttons.length; i++) {
				if (m_buttons[i].doClick(x - m_show_x, y - m_show_y)) {
					m_index_menu = i;
					if (m_listener != null) {
						m_listener.OnClick();
					}
				}
			}
//			if (m_strs != null) {
//				m_index_menu = (int)(y - m_show_y) / (DWControlsManager.H_ALL_FONT + (IH * 2));
//				if (m_index_menu >= m_strs.length) {
//					m_index_menu = m_strs.length - 1;
//				} else if (m_index_menu < 0) {
//					m_index_menu = 0;
//				}
//				if (m_listener != null) {
//					m_listener.OnClick();
//					return true;
//				}
//			}
			return true;
		}
		else {
			destruct();
		}
		return true;
	}

}
